<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户 ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="coinsList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户 ID" align="center" prop="userId" />
      <!-- <el-table-column label="部分" align="center" prop="part" /> -->
      <el-table-column label="难度" align="center" prop="difficulty" />
      <el-table-column label="获得金币数" align="center" prop="fraction" />
      <el-table-column label="正确题目数" align="center" prop="rightNum" />
      <el-table-column label="总题目数" align="center" prop="allNum" />
      <!-- <el-table-column label="次数" align="center" prop="times" /> -->
      <el-table-column
        label="开始时间"
        align="center"
        prop="beginTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.beginTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.endTime">{{
            parseTime(scope.row.endTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="时长" align="center" prop="duration" />
      <el-table-column label="正确率" align="center" prop="accuracy" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import request from "../../../utils/request";
import * as XLSX from "xlsx";

export default {
  name: "tongji",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      coinsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "平台id不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    format(list) {
      return list.map((item) => {
        if (item.difficulty == 0) {
          item.difficulty = "简单";
        } else if (item.difficulty == 1) {
          item.difficulty = "中等";
        } else if (item.difficulty == 2) {
          item.difficulty = "困难";
        }
        if (item.accuracy !== null)
          item.accuracy = (item.accuracy * 100).toFixed(0) + "%";
        return item;
      });
    },
    getList() {
      this.loading = true;
      request({
        url: "/north/data/p2",
        method: "post",
        params: this.queryParams,
      }).then((res) => {
        console.log(res);
        this.coinsList = this.format(res.rows);
        this.total = res.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // handleSelectionChange(val) {
    //   console.log("选中的行: ", val);
    //   // 在这里添加你的处理逻辑
    // },
    /** 导出按钮操作 */
    handleExport() {
      // 发送请求获取数据
      request({
        url: "/north/data/p2",
        method: "post",
        params: {
          userId: this.queryParams.userId,
          pageNum: 1,
          pageSize: 100000,
        },
      }).then((res) => {
        let dataList = this.format(res.rows).map((item) => {
          return {
            日期: item.date,
            "用户 ID": item.userId,
            难度: item.difficulty,
            获得金币数: item.fraction,
            正确题目数: item.rightNum,
            总题目数: item.allNum,
            开始时间: item.beginTime,
            结束时间: item.endTime,
            时长: item.duration,
            正确率: item.accuracy,
          };
        });
        // this.total = res.total;
        this.loading = false;

        // 处理数据
        const ws = XLSX.utils.json_to_sheet(dataList); // 将 JSON 数据转换为工作表
        const wb = XLSX.utils.book_new(); // 创建一个新的工作簿
        XLSX.utils.book_append_sheet(wb, ws, "Sheet1"); // 将工作表添加到工作簿中

        // 导出 Excel 文件
        XLSX.writeFile(wb, "数据导出.xlsx"); // 使用浏览器下载 Excel 文件
      });
    },
  },
};
</script>
